#!/usr/bin/make -f

# Copyright (c) 2015, 2025, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

%:
	dh $@

export DH_VERBOSE=1

ifeq (,$(WITH_PGO))
	PROFILE=
else
	PROFILE=-DFPROFILE_USE=1
endif

ifeq (,$(MYSQL_BUILD_MAKE_JFLAG))
	JOBS=-j8
else
	JOBS=$(MYSQL_BUILD_MAKE_JFLAG)
endif


override_dh_auto_configure:
	@echo "RULES.$@"

	@DEB_RULES_DEBUG_CMAKE@

	@DEB_RULES_PROFILE_GENERATE@

	mkdir release && cd release && \
	cmake .. \
		$(PROFILE) \
		-DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINSTALL_LIBDIR=lib/$(DEB_HOST_MULTIARCH) \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_UNIX_ADDR=/var/run/mysqld/mysqld.sock \
		-DWITH_MECAB=system \
		-DWITH_ZLIB=@DEB_ZLIB_OPTION@ \
		-DWITH_NUMA=ON \
		-DCOMPILATION_COMMENT="MySQL @DEB_PRODUCTNAMEC@ - @DEB_LICENSENAME@" \
		-DCOMPILATION_COMMENT_SERVER="MySQL @DEB_PRODUCTNAMEC@ Server - @DEB_LICENSENAME@" \
		-DINSTALL_LAYOUT=DEB \
		-DREPRODUCIBLE_BUILD=OFF \
		-DDEB_PRODUCT=@DEB_PRODUCT@ \
		@DEB_CMAKE_EXTRAS@

	touch $@

override_dh_auto_build:
	@echo "RULES.$@"
	@DEB_RULES_DEBUG_MAKE@
	cd release && \
	$(MAKE) $(JOBS) VERBOSE=1
	touch $@

# timeout in seconds
override_dh_auto_test:
	@echo "RULES.$@"
	cd release && \
	make test-unit || true
	touch $@

@DEB_RULES_STRIP@
@DEB_NDB_RULES_STRIP@

override_dh_auto_install:

	@echo "RULES.$@"
	# complete install first
	cd release && \
	$(MAKE) install DESTDIR=../debian/tmp
	@DEB_RULES_DEBUG_EXTRA@
	# Add helper functions for maintainer scripts
	install -m 0644 debian/extra/mysql-helpers debian/tmp/usr/share/mysql-@MYSQL_BASE_VERSION@/
	# add apparmor profile
	@DEB_RULES_INSTALL_APPARMOR@
	@DEB_RULES_INSTALL_APPARMOR_ROUTER@
	# add systemd scripts
	install -m 0755 debian/extra/mysql-systemd-start debian/tmp/usr/share/mysql-@MYSQL_BASE_VERSION@/
	@DEB_RULES_INSTALL_ROUTER_SYSTEMD@
	touch $@

override_dh_installinit:
	@echo "RULES.$@"
	@DEB_RULES_APPARMOR_LOAD@
	@DEB_RULES_APPARMOR_LOAD_ROUTER@
	dh_installsystemd --name=mysql
	dh_installsystemd --name=mysql@ --no-enable --no-start
	@DEB_RULES_INSTALLINIT_ROUTER@
	touch $@

override_dh_install:
	dh_install --fail-missing \
		-Xusr/bin/i_mysql_client_test \
		-Xmysql.server \
		-XChangeLog \
		-Xusr/share/man/man1/mysqlman.1

# Since this dynamic library is included in both server and router, stripping
# it leads to file conflict errors when trying to install dbgsym-deb packages
# for both.
override_dh_strip:
	dh_strip -Xlibprotobuf-lite -Xlibmysqlclient.a -Xlibabsl_ -Xlibicu

# dh_strip_nondeterminism makes changes to archive files that actually makes
# the build less deterministic because we can't verify them against what's in git
override_dh_strip_nondeterminism:
	dh_strip_nondeterminism -X.zip -X.gzip

# Debug plugins use libprotobuf-lite from the server package.
override_dh_shlibdeps:
	dh_shlibdeps -Lmysql-@DEB_PRODUCT@-server-core -l/usr/lib/mysql/private
